package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import com.lagou.utils.DateUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {
    /**
     * 根据课程id查找对应的章节和章节对应的视频
     * @param request
     * @param response
     */
    public void findSectionAndLessonByCourseId(HttpServletRequest request , HttpServletResponse response){
        PrintWriter writer = null;
        try {
            String course_id = request.getParameter("course_id");
            CourseContentService courseContentService = new CourseContentServiceImpl();
            List<Course_Section> sectionAndLessons = courseContentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));
            String result = JSON.toJSONString(sectionAndLessons);
            writer = response.getWriter();
            writer.print(result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }
    }

    /**
     * 根据课程id查找课程
     * @param request
     * @param response
     */
    public void findCourseById(HttpServletRequest request , HttpServletResponse response){
        PrintWriter writer = null;
        try {
            String course_id = request.getParameter("course_id");
            CourseContentService courseContentService = new CourseContentServiceImpl();
            Course course = courseContentService.findCourseByCOurseId(Integer.parseInt(course_id));
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name");
            String result = JSON.toJSONString(course,filter);
            writer = response.getWriter();
            writer.print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            writer.close();
        }
    }

    /**
     * 保存或者更新课程
     * @param request
     * @param response
     */
    public void saveOrUpdateSection(HttpServletRequest request ,HttpServletResponse response){
        CourseContentService courseContentService = new CourseContentServiceImpl();
        Course_Section section = new Course_Section();
        Map<String,Object> map = (Map<String,Object>)request.getAttribute("map");
        PrintWriter writer = null;
        String result = null;
        try {
            BeanUtils.populate(section,map);
            if(section.getId() == 0){
                 result = courseContentService.saveSection(section);
            }else{
                 result = courseContentService.updateSection(section);
            }
            writer = response.getWriter();
            writer.print(result);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            writer.close();
        }
    }

    /**
     * 跟新课程状态
     * @param request
     * @param response
     */
    public void updateSectionStatus(HttpServletRequest request , HttpServletResponse response){
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        PrintWriter writer = null;
        CourseContentService courseContentService = new CourseContentServiceImpl();
        String result = courseContentService.updateSectionStatus(Integer.parseInt(id), Integer.parseInt(status));
        try {
            writer = response.getWriter();
            writer.print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            writer.close();
        }
    }

    /**
     * 保存或修改方法
     * @param request
     * @param response
     */
    public void saveOrUpdateLesson(HttpServletRequest request , HttpServletResponse response){
        CourseContentService courseContentService = new CourseContentServiceImpl();
        PrintWriter writer = null;
        String id = request.getParameter("id");
        String course_id = request.getParameter("course_id");
        String section_id = request.getParameter("section_id");
        String theme = request.getParameter("theme");
        String duration = request.getParameter("duration");
        String is_free = request.getParameter("is_free");
        String order_num = request.getParameter("order_num");
        Course_Lesson lesson = new Course_Lesson();
        lesson.setCourse_id(Integer.parseInt(course_id));
        lesson.setSection_id(Integer.parseInt(section_id));
        lesson.setTheme(theme);
        lesson.setDuration(Integer.parseInt(duration));
        lesson.setIs_free(Integer.parseInt(is_free));
        lesson.setOrderNum(Integer.parseInt(order_num));
        String formart = DateUtils.getDateFormart();
        lesson.setUpdate_time(formart);
        try {
            writer = response.getWriter();
            if(id == null){
                lesson.setCreate_time(formart);
                String result = courseContentService.saveLesson(lesson);
                writer.print(result);
            }else{
                lesson.setId(Integer.parseInt(id));
                String result = courseContentService.updateLesson(lesson);
                writer.print(result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }
    }
}
